using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetRelationships : UserControl, DemoControl
    {
        public RichTextBox oTextBox;
        private bool bUserEntityMode = false;
        
        public ctrlGetRelationships(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;
            EnableProcessButton();
        }

        public void setUserEntityMode(bool bMode)
        {
            bUserEntityMode = bMode;
            if (bMode)
            {
                grpResults.Text = "Get Relationships (User-Defined Entities)";
                EnableProcessButton();
            }
            else
            {
                grpResults.Text = "Get Relationships (Named Entities)";
                EnableProcessButton();
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }


        public void processText()
        {
            gridResults.Rows.Clear();

            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                List<SalienceRelationship> oResults;
                if (!bUserEntityMode)
                {
                    oResults = SalienceDemo.oSalienceSession.GetNamedEntityRelationships(SalienceDemo.oSalienceSession.EntityThreshold);
                }
                else
                {
                    oResults = SalienceDemo.oSalienceSession.GetUserEntityRelationships();
                }

                if (oResults.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceRelationship oRelationship in oResults)
                    {
                        gridResults.Rows.Add();
                        gridResults.Rows[nCount].Cells[0].Value = oRelationship.sLabel;
                        gridResults.Rows[nCount].Cells[1].Value = oRelationship.fScore;
                        gridResults.Rows[nCount].Cells[2].Value = oRelationship.Entities[0].sNormalizedForm;
                        if (oRelationship.Entities.Count > 1)
                        {
                            gridResults.Rows[nCount].Cells[3].Value = oRelationship.Entities[1].sNormalizedForm;
                        }
                        else
                        {
                            gridResults.Rows[nCount].Cells[3].Value = "";
                        }
                        if (oRelationship.Entities.Count > 2)
                        {
                            gridResults.Rows[nCount].Cells[4].Value = oRelationship.Entities[2].sNormalizedForm;
                        }
                        else
                        {
                            gridResults.Rows[nCount].Cells[4].Value = "";
                        }
                        gridResults.Rows[nCount].Cells[5].Value = oRelationship.sExtra;
                        nCount++;
                    }
                    gridResults.AutoResizeRows();
                }
                else
                {
                    if (bUserEntityMode)
                        MessageBox.Show("No Relationships Available", "GetUserEntityRelationships()");
                    else
                        MessageBox.Show("No Relationships Available", "GetNamedEntityRelationships()");
                    
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //Clean up
        private void ctrlGetRelationships_VisibleChanged(object sender, EventArgs e)
        {
            gridResults.Rows.Clear();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.DOCUMENTS);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.gridResults.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridResults.AutoResizeRows();
        }

        public void EnableProcessButton()
        {
            if (bUserEntityMode)
            {
                this.btnProcess.Enabled = (System.IO.File.Exists(SalienceDemo.oConfig.activeSettings().settings["UserEntityList"])) && (oTextBox.Text.Trim().Length > 0);
            }
            else
            {
                this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
            }
        }

        #endregion
    }
}
